package top.program.controller;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import top.program.entity.Student;
import top.program.entity.Teacher;
import top.program.entity.User;
import top.program.service.StudentService;
import top.program.service.TeacherService;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

@Controller()
@RequestMapping("user")
public class UserController {
    @Autowired
    private StudentService studentService;
    @RequestMapping("/test")
    public String userTest(@RequestBody Student student) throws IOException {
//        String name = file.getOriginalFilename();
//        File uploadF = new File("D:/"+name);
//        file.transferTo(uploadF);
//        System.out.println(user.getName());
//        String id = request.getParameter("id");
//        String name = request.getParameter("name");
//        User user = new User(12,"老王");
//        modelMap.addAttribute("user",user );
        studentService.createStudent(student);
        return "user";
    }

    @RequestMapping("/demo")
    public String userDemo() {

        return "demo";
    }
    @RequestMapping("/down")
    public ResponseEntity<byte[]> down() throws IOException {
        String path = "D:\\1.jpg";
        File file = new File(path);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentDispositionFormData("attachment",path);
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers,HttpStatus.CREATED);
    }
}
